package com.hundsun.fcp.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.hundsun.fcp.R;
import com.hundsun.fcp.base.BaseActivity;
import com.hundsun.fcp.common.Redirect;
import com.hundsun.fcp.common.utils.StringUtils;
import com.hundsun.fcp.widget.EditTextWithClear;

public class MineResetTransPwdActivity extends BaseActivity implements
		OnClickListener {
	private EditTextWithClear mIdNumber;
	private EditTextWithClear mSmsCode;
	private EditTextWithClear mNewPwd;
	private EditTextWithClear mConfirmPwd;
	private Button mSubmit;
	private Button mSmsButton;

	private String strIDNumber;
	private String strSmsCode;
	private String strNewPwd;
	private String strConfirmPwd;

	protected int getContentLayout() {
		return R.layout.fcp_mine_reset_transpwd;
	}

	protected void initHeader() {
		super.initHeader();
		mHeaderMenu.setTitle(R.string.mine_reset_trans_pwd);
	}

	public void initView() {
		super.initView();
		mIdNumber = (EditTextWithClear) findViewById(R.id.et_IDcard);
		mSmsCode = (EditTextWithClear) findViewById(R.id.id_msgcode_modify_ipt);
		mNewPwd = (EditTextWithClear) findViewById(R.id.et_newpwd);
		mConfirmPwd = (EditTextWithClear) findViewById(R.id.et_confirmpwd);
		mSmsButton = (Button) findViewById(R.id.id_obtain_msgcode_modify_btn);
		mSubmit = (Button) findViewById(R.id.btn_submit);

		mSmsCode.addTextChangedListener(mTextWatcher);
		mNewPwd.addTextChangedListener(mTextWatcher);
		mConfirmPwd.addTextChangedListener(mTextWatcher);

		mSmsButton.setOnClickListener(this);
		mSubmit.setOnClickListener(this);

		mIdNumber.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
				setMsgCodeBtnEnabled();
				vfySignBtn();
			}
		});
	}

	private TextWatcher mTextWatcher = new TextWatcher() {

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		public void afterTextChanged(Editable s) {
			vfySignBtn();
		}
	};

	public void onClick(View v) {
		if (v == mSmsButton) {
			// 已经验证非空 可直接使用
			strIDNumber = mIdNumber.getText().toString().trim();
			// doObtainSms();
		} else if (v == mSubmit) {
			// 已经验证非空 可直接使用
			strIDNumber = mIdNumber.getText().toString().trim();
			strSmsCode = mSmsCode.getText().toString().trim();
			strNewPwd = mNewPwd.getText().toString().trim();
			strConfirmPwd = mConfirmPwd.getText().toString().trim();
			if (strNewPwd.equals(strConfirmPwd)) {
				// doSubmit();
			} else {
				// 显示错误信息
				Redirect.makeToast(MineResetTransPwdActivity.this,
						R.string.pwd_confirm_error);
			}
		}
	}

	/**
	 * 根据身份证号是否为空来控制 获取验证码 按钮
	 */
	private void setMsgCodeBtnEnabled() {
		strIDNumber = mIdNumber.getText().toString().trim();
		if (strIDNumber.length() == 18) {
			mSmsButton.setEnabled(true);
			mSmsButton.setBackgroundResource(R.color.msgcode_bg);
		} else {
			mSmsButton.setEnabled(false);
			mSmsButton.setBackgroundResource(R.color.signup_msgcode_bg);
		}

	}

	/**
	 * 根据是否输入身份证号、验证码、新密码和确认新密码来控制提交按钮
	 */
	private void vfySignBtn() {
		strIDNumber = mIdNumber.getText().toString().trim();
		strSmsCode = mSmsCode.getText().toString().trim();
		strNewPwd = mNewPwd.getText().toString().trim();
		strConfirmPwd = mConfirmPwd.getText().toString().trim();

		if (StringUtils.isEmpty(strIDNumber) || StringUtils.isEmpty(strSmsCode)
				|| StringUtils.isEmpty(strNewPwd)
				|| StringUtils.isEmpty(strConfirmPwd)) {
			mSubmit.setEnabled(false);
			mSubmit.setBackgroundResource(R.drawable.btn_grey_unable);
		} else {
			mSubmit.setEnabled(true);
			mSubmit.setBackgroundResource(R.drawable.btn_red_normal);
		}
	}

}
